﻿/**
 * Rect
 *
 * @author		Allen Chou
 * @version		1.1.1 (last update: Dec 15 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.display.shapes {
	import flash.display.*;
	import flash.events.*;
	
	import idv.cjcat.display.*;
	
	/**
	 * The <code>Rect</code> class lets you conveniently creates a rectangle shape,
	 * whose style can be easily set by using <code>LineStyle</code> and <code>Fill</code>.
	 */
	public class Rect extends AbstractShape {
		/**
		 * The width of the rectangle.
		 */
		public var rectWidth:Number;
		/**
		 * The height of the rectangle.
		 */
		public var rectHeight:Number;
		/**
		 * Whether the origin of the rectangle is used as its top left corner or center.
		 */
		public var topLeftAlign:Boolean;
		
		/**
		 * Creates a rectangle.
		 * @param	rectWidth     The width of the rectangle.
		 * @param	rectHeight    The height of the rectangle.
		 * @param	topLeftAlign  Whether the origin of the rectangle is used as its top left corner or center.
		 * @param	fill          The fill of the rectangle.
		 * @param	lineStyle     The line style of the rectangle.
		 */
		public function Rect(rectWidth:Number = 100, rectHeight:Number = 75, topLeftAlign:Boolean = false) {
			this.rectWidth = rectWidth;
			this.rectHeight = rectHeight;
			this.topLeftAlign = topLeftAlign;
			render();
		}
		
		/**
		 * Call this method after modifications to properties are made to update appearance.
		 * <p>This method can be used as an event listener.</p>
		 */
		public override function render(e:Event = null):void {
			graphics.clear();
			if ((this.lineStyle != null) || (this.lineStyle.alpha == 0)) CJDisplay.lineStyle(this, lineStyle);
			if ((this.fill != null) || (this.fill.alpha == 0)) CJDisplay.beginFill(this, fill);
			if (this.topLeftAlign) {
				graphics.drawRect(0, 0, this.rectWidth, this.rectHeight);
			} else {
				graphics.drawRect( -this.rectWidth / 2, -this.rectHeight / 2, this.rectWidth, this.rectHeight);
			}
		}
		
	}
}